package orchestration.businessprocess.model;

import java.util.HashMap;
import java.util.Set;

import orchestration.businessprocess.model.nodes.BPNode;


/**
 * Implementation of {@link BusinessProcessModel}
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class BPMImpl implements BusinessProcessModel {

	/**
	 * @uml.property  name="startId"
	 * @uml.associationEnd  
	 */
	private BPNodeId startId;
	/**
	 * @uml.property  name="nodes"
	 */
	private HashMap<BPNodeId,BPNode> nodes;
	/**
	 * @uml.property  name="artifacts"
	 */
	private HashMap<BPArtifactId, BPArtifact> artifacts;
	/**
	 * @uml.property  name="annotations"
	 */
	private HashMap<String, BPAnnotation> annotations;
	
	public BPMImpl(BPNodeId startId,HashMap<BPNodeId, BPNode> nodes,
			HashMap<BPArtifactId, BPArtifact> artifacts) {
		super();
		this.startId=startId;
		this.nodes = nodes;
		this.artifacts = artifacts;
	}

	/**
	 * retrieves a {@link BPArtifact}
	 * @param artifactId the {@link BPArtifactId} that identifies the artifact
	 * @return the artifact, if there was one with the given artifactId
	 */
	@Override
	public BPArtifact getArtifact(BPArtifactId artifactId) {
		return artifacts.get(artifactId);
	}

	@Override
	public BPNode getNode(BPNodeId nodeId) {
		return nodes.get(nodeId);
	}

	@Override
	public Set<BPArtifact> getInput(BPNodeId nodeId) {
		BPNode node=getNode(nodeId);
		if(node.getNodeType()!=BPNode.NODE_TYPE_TASK)
			return null;
		return ((BPNode)node).getInput();
	}


	@Override
	public Set<BPArtifact> getOutput(BPNodeId nodeId) {
		BPNode node=getNode(nodeId);
		if(node.getNodeType()!=BPNode.NODE_TYPE_TASK)
			return null;
		return ((BPNode)node).getOutput();
	}

	@Override
	public BPNodeId getStartNodeId() {
		return startId;
	}
	
	public BPAnnotation getAnnotation(String id){
		return annotations.get(id);
	}

	/**
	 * retrieves an Artifact given its name
	 */
	@Override
	public BPArtifact getArtifactByName(String name) {
		BPArtifact result=null;
		for(BPArtifact artifact : artifacts.values()){
			if(artifact.getName().equals(name))
				result=artifact;
		}
		return result;
	}

	@Override
	public BPNode getStartNode() {
		return nodes.get(startId);
	}

}
